﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

//using SIPO.Library.DomainObject.Administration.Catalog;
//using SIPO.Library.DataAccess.Administration.Catalog;
using System.Configuration;
using Assesment.Web.Support;
using Assesment.Library.DomainObject.Administration.UserManagement;
using Assesment.Library.DomainObject.Administration.Catalog;
using Assesment.Library.DataAccess.Administration.Catalog;
using SIPO.Library.DomainObject.Administration.Catalog;


namespace Assesment.Web.Forms.Administration.Catalog
{

    public partial class KeyBehaviourForm : BasePage
    {
        private int id = -1;


        protected override void OnLoad(EventArgs e)
        {
            try
            {
                if (Request.Params["id"] != null)
                {
                    id = Convert.ToInt32(Request.Params["id"]);
                }

                if (!Page.IsPostBack)
                {


                    if (id > 0) // Edit an existing item
                    {
                        KeyBehaviourRepo KeyBehaviourDal = new KeyBehaviourRepo(ConfigurationManager.ConnectionStrings["AssesmentConn"].ConnectionString);
                        KeyBehaviour obj = KeyBehaviourDal.GetById(id);
                        if (obj != null)
                        {
                            hfID.Value = id.ToString();
                           

                            CompetencyRepo CompetencyDal = new CompetencyRepo(ConfigurationManager.ConnectionStrings["AssesmentConn"].ConnectionString);
                            Competency objCompetency = CompetencyDal.GetById(Convert.ToInt32(obj.CompetencyID));
                            if (objCompetency != null)
                            {
                                txtDefinition.Text = objCompetency.Definition;

                                List<Competency> listComp = CompetencyDal.GetList();
                                var query = listComp.Where(x => x.GroupCompetency == objCompetency.GroupCompetency);
                                listComp = query.ToList();
                                ddlKompetensi.Items.Clear();
                                listComp.ForEach(delegate(Competency objCompt)
                                {
                                    ddlKompetensi.Items.Add(new ListItem(objCompt.CompetncyDesc, objCompt.ComptID.ToString()));
                                });

                                ddlKompetensi.Items.Insert(0, new ListItem("-- pilih kompetensi --", string.Empty));

                                ddlKompetensi.Items.FindByValue(obj.CompetencyID.ToString()).Selected = true;

                                ddlGroup.Items.FindByValue(objCompetency.GroupCompetency.ToString()).Selected = true;

                                LevelCompetencyRepo LevelCompetencyDal = new LevelCompetencyRepo(ConfigurationManager.ConnectionStrings["AssesmentConn"].ConnectionString);
                                LevelCompetency objLevel = LevelCompetencyDal.GetById(Convert.ToInt32(obj.LevelCompetencyID));
                                if (objLevel != null)
                                {
                                    txtDeskripsiLevel.Text = objLevel.Definition;

                                    List<LevelCompetency> listLevel = LevelCompetencyDal.GetList();
                                    var queryLevel = listLevel.Where(x => x.CompetencyID == objCompetency.ComptID);
                                    listLevel = queryLevel.ToList();
                                    ddlLevel.Items.Clear();
                                    listLevel.ForEach(delegate(LevelCompetency objLvl)
                                    {
                                        ddlLevel.Items.Add(new ListItem(objLvl.Level.ToString(), objLvl.ID.ToString()));
                                    });

                                    ddlLevel.Items.Insert(0, new ListItem("-- pilih level --", string.Empty));

                                    ddlLevel.Items.FindByValue(obj.LevelCompetencyID.ToString()).Selected = true;
                                }
                            }

                           

                        }
                    }
                    GetPrivilege();
                }
            }
            catch
            {

            }

        }

        private void GetPrivilege()
        {
            Membership membership = new Membership(ConfigurationManager.ConnectionStrings["AssesmentConn"].ConnectionString);

            List<recAccessRights.AccessRights> accessRights = membership.GetAccessRights(base.UserID, base.MenuID);

            accessRights.ForEach(delegate(recAccessRights.AccessRights access)
            {
                switch (access)
                {
                    case (recAccessRights.AccessRights.View):
                        ClientScript.RegisterStartupScript(typeof(Page), "view", "$('#btnSave').button('disable');", true);
                        break;
                    case (recAccessRights.AccessRights.Add):
                        ClientScript.RegisterStartupScript(typeof(Page), "add", "$('#btnSave').button('enable');", true);
                        break;
                    case (recAccessRights.AccessRights.Edit):
                        ClientScript.RegisterStartupScript(typeof(Page), "edit", "$('#btnSave').button('enable');", true);
                        break;
                    case (recAccessRights.AccessRights.NoAccess):
                        var url = Helper.GetAppVirtualPath(Page);
                        ClientScript.RegisterStartupScript(typeof(Page), "noAccess", "alert('Anda tidak mempunyai hak akses ke halaman ini!'); window.location = '" + url + "'", true);
                        break;
                }
            });

        }

    
    }

}
